#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    int T = 0;
    cin >> T;
    while (T--)
    {
        int start = 0;
        int n = 0, k = 0;
        cin >> n >> k;
        string mili;
        cin >> mili;
        int win = 0;
        for (int i = 0;i < n;i++)
        {
            if (mili[i] == 'W')
            {
                win++;
                if (win >= 3)
                {
                    start += k;
                }
                else
                {
                    ++start;
                }
            }
            else
            {
                win = 0;
                --start;
            }
        }
        cout << start << endl;
    }
    return 0;
}